let wxMarkerData = []; //  定位成功回调对象
let bmap = require('../libs/bmap-wx.min.js');

const app = getApp()

Page({
  data: {
    ak: "gSGgviuSsGGF9iiqOpXIuloUKwjauBbf", //  ak
    markers: [],
    longitude: '', //  经度
    latitude: '', //  纬度
    address: '', //  地址
    cityInfo: {}, //  城市信息
    ugetaddress: "请选择地址", //默认是选择地址，位置选择后将具体位置存储到这里
    name : '', // 姓名
    Phone : '', // 手机号
    datis : '' , // 详细地址
  },
  onLoad: function (options) {
    // 获取当前位置
    var that = this;
    let BMap = new bmap.BMapWX({
      ak: that.data.ak
    });
    let fail = function (data) {
      console.log(data);
    };
    let success = function (data) {
      // 返回数据内，已经包含经纬度
      // 使用wxMarkerData获取数据
      wxMarkerData = data.wxMarkerData;
      // 把所有数据放在初始化data内
      that.setData({
        markers: wxMarkerData,
        latitude: wxMarkerData[0].latitude,
        longitude: wxMarkerData[0].longitude,
        address: wxMarkerData[0].address,
        cityInfo: data.originalData.result.addressComponent
      });
    };
    // 发起regeocoding检索请求
    BMap.regeocoding({
      fail: fail,
      success: success
    });
  },
  ins : function(data){
    let names = data.currentTarget.dataset.a
    let value = data.detail.value
    this.setData({
      [names] : value
    })
  },
  openmap(e) {
    var that = this;
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        var ugetaddress = 'userData.ugetaddress'
        wx.chooseLocation({
          success: function (res) {
            var address_name = res.name;
            var address_info = res.address;
            that.setData({
              latitude: res.latitude,
              longitude: res.longitude,
              address: address_info //将解析后的地址进行存储
            })
          },
          fail: function () {
            console.log(res);
          },
          complete: function () {
            // complete
          }
        })
      }
    })
  },
  subMit() {
    this.userAd()
    wx.request({
      url: 'http://localhost:8081/interfaced/address/add',
      data: {
        shopName: this.data.name,
        ShopPhone: this.data.Phone,
        ShopAddress: this.data.address,
        ShopDetails: this.data.datis
      },
      success(data) {
        wx.reLaunch({
          url: '../my/index',
        })
      }
    })
  },
  userAd(){ // 添加用户
    wx.request({
      url: 'http://localhost:8081/interfaced/user/add',
      data : {
        wxOpenid : app.globalData.openid,
        name : this.data.name,
        phone : this.data.Phone,
        address : this.data.address
      },
      success(data){
      }
    })
  }
})